﻿using System;
using System.Linq;
using System.Threading;
using log4net;
using Quartz;
using TdxApiPool.Connect;

namespace TdxApiPool.Task
{
    [DisallowConcurrentExecution]
    public class ReConnectTask : IJob
    {

        private static readonly ILog Log = LogManager.GetLogger(typeof(ReConnectTask));
        public void Execute(IJobExecutionContext context)
        {
            Reconnect();
        }

        private void Reconnect()
        {
            Log.Info("------------------------------");
            Log.Info("start ReConnectTask.Reconnect");
            
            var unAvailabeConnections = ConnectPool.GetInstance().GetUnAvailabeConnections().Where(m => m.ConnectTime < 10);
            foreach (var unAvailabeConnection in unAvailabeConnections)
            {
                bool connect = unAvailabeConnection.Connect();
                Log.Info(
                    $"name = {unAvailabeConnection.ConnectionName}\nip = {unAvailabeConnection.Ip}:{unAvailabeConnection.Port}\nreconnect = {connect}\n");
            }
            Log.Info("end ReConnectTask.Reconnect");
            Log.Info("------------------------------");
        }
    }
}